<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* 	Modèle pour la table Region
**/


class Events extends CI_Model
{
	private $lat;
	private $lng;
	public function find_coordinate()
	{
	  $this->error=false;
	     $host="maps.google.com";
	     $key=google_api_key;// recuperer le key du fichier de config
		
	    $Base_url = "http://".$host."/maps/geo?output=json&key=".$key;
	    $requette=$Base_url."&sensor=false&q=".urlencode($this->adresse);
		
		$contenu = file_get_contents($requette);
		$contenu = json_decode($contenu,true);
		if (isset($contenu["Placemark"])){
			$cordinate = $contenu["Placemark"][0]["Point"]["coordinates"];
			$this->lat = $cordinate[1];
			$this->lng = $cordinate[0];
		}else{
			$this->lat = 0;
			$this->lng = 0;
	                $this->error=true;
		}
		
	}

	public static function getType_event_visiteur()
	{
		$list_event= array();
		$CI =& get_instance();	
		if($CI->session->userdata('visiteur_type'))
		{
			echo $CI->session->userdata('visiteur_type');
			// event type

			$list_event = array();
			$sql ="select * from website_type_event";
			
			$query = $CI->db->query($sql,array());
			$row = $query->result_array();
			$list_event = $row;
			$query->free_result();
			// type lieu
			
			
		}
		return $list_event;
	}
	

	public  static function getEvent_by_region()
	{
		if(isset($_POST['region']))
			{
				
				
				$sql = 'SELECT * FROM  website_event  left join  website_lieu on website_event.lieu = website_lieu.id  left join  website_ville on website_lieu.ville = website_ville.id left join website_departement on website_ville.departement = website_departement.id left join website_region on website_departement.region = website_region.id  WHERE website_region.nom = ?';
				
				$CI =& get_instance();	
				$query = $CI->db->query($sql,array($key));
				$row = $query->result_array();
				
				

				$query->free_result();
			}
			
		return $row;
	}

	public static function getEvent_by_type()
	{
		$row = array();
		if(isset($_POST['region'],$_POST['type_event']))
			{
				$event = new Event();
				
				$sql = 'SELECT * FROM  website_event  left join website_event_types on website_event_types.event_id = website_event.id left join website_type_event on website_type_event.id = website_event_types.type_event_id  left join  website_lieu on website_event.lieu_id = website_lieu.id  left join  website_ville on website_lieu.ville_id = website_ville.id left join website_departement on website_ville.departement_id = website_departement.id left join website_region on website_departement.region_id = website_region.id  WHERE website_region.nom = ? and website_type_event.nom = ?';
				
				$CI =& get_instance();	
				$query = $CI->db->query($sql,array($_POST['region'],$_POST['type_event']));
				$row = $query->result_array();
				
				
				$query->free_result();
			}
			
		return $row;
	}

	public static function getAllType_event()
	{
		$row = array();
					$sql = 'SELECT * FROM  website_type_event ';
					
					$CI =& get_instance();	
					$query = $CI->db->query($sql);
					$row = $query->result_array();
					
					

					$query->free_result();
					return $row;
	}


	

}